import Vue from 'vue'
import Router from 'vue-router'
import login from '../views/auth/login'
import error from '../views/error/error'
import home from '../views/home'
import dashboard from '../views/dashboard/dashboard'
import ApplyEdit from '../views/ApplyEdit.vue'
import ApplySearch from '../views/ApplySearch.vue'
import ApplyInfo from '../views/ApplyInfo.vue'
import TaskList from '../views/TaskList.vue'
import info from '../views/info/info.vue'
import preApprove from '../views/PreApprove'
import danmu from '../views/info/danmu'
import editLoan from '../views/loan/editLoan'
// import mqtt from '../views/info/mqtt'

Vue.use(Router)

// login、error、home为全局页面 切换路由，home有子路由，子路由切换不刷整个页面
const router = new Router({
  routes: [
    { name: 'login', path: '/login', component: login },
    { name: 'error', path: '/error', component: error },
    { name: 'home',
      path: '/',
      component: home,
      meta: {
        requireAuth: true
      },
      children: [
        { path: 'applysearch', name: '申请查询', component: ApplySearch },
        { path: 'applyedit/:id', name: '新建申请', component: ApplyEdit },
        { path: 'applyinfo/:id', name: '申请信息', component: ApplyInfo },
        { path: 'info/:id', name: '密集信息', component: info },
        { path: 'dashboard', name: '仪表板', component: dashboard, meta: { requireAuth: true } },
        { path: 'tasklist', name: '工作台', component: TaskList, meta: { requireAuth: true } },
        { path: 'preApprove', name: '快速预审批', component: preApprove, meta: { requireAuth: true } },
        { path: 'danmu', name: '弹幕', component: danmu, meta: { requireAuth: true } },
        { path: 'editLoan/:id', name: '修改申请', component: editLoan, meta: { requireAuth: true } }
        // { path: 'mqtt', name: '快速预审批', component: mqtt, meta: { requireAuth: true } }
      ]
    }
  ],
  linkExactActiveClass: 'nav-item active'
})

// 路由生命周期钩子函数，在路由跳转前检查前台是否有token，若没有返回登陆画面
/* router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (getLocalStorage('admin-token')) {
      next()
    } else {
      next({
        path: 'login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
}) */

export default router
